package com.dongdongshop.service.impl;

import cn.hutool.json.JSONUtil;
import com.dongdongshop.domain.model.*;
import com.dongdongshop.domain.vo.AddressVO;
import com.dongdongshop.mapper.TbAddressMapper;
import com.dongdongshop.mapper.TbAreasMapper;
import com.dongdongshop.mapper.TbCitiesMapper;
import com.dongdongshop.mapper.TbProvincesMapper;
import com.dongdongshop.service.AddressService;
import com.dongdongshop.util.JwtUtil;
import com.dongdongshop.util.UserVO;
import io.jsonwebtoken.Claims;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class AddressServiceImpl implements AddressService {

    @Autowired
    private TbAddressMapper addressMapper;

    @Autowired
    private TbProvincesMapper provincesMapper;

    @Autowired
    private TbCitiesMapper citiesMapper;

    @Autowired
    private TbAreasMapper areasMapper;

    @Override
    public void addAddress(AddressVO addressVO, String autharizeToken) {
        //获取到用户的username
        Claims claims = JwtUtil.parseJwt(autharizeToken);
        String subject = claims.getSubject();//用户对象
        UserVO userVO = JSONUtil.toBean(subject, UserVO.class);//转成uservo类型的数据
        String username = userVO.getName();
        addressVO.setUserId(username);//把username作为userid放进去
        addressVO.setIsDefault("0");//默认不是作为默认地址
        addressVO.setCreateDate(new Date());//设置创建日期, 默认当前时间
        //将AddressVO转成TbAddress
        TbAddress tbAddress = new TbAddress();
        BeanUtils.copyProperties(addressVO,tbAddress);
        //增加
        addressMapper.insertSelective(tbAddress);
    }

    @Override
    public List<AddressVO> selectArea(String autharizeToken) {
        //获取到用户的username
        Claims claims = JwtUtil.parseJwt(autharizeToken);
        String subject = claims.getSubject();//用户对象
        UserVO userVO = JSONUtil.toBean(subject, UserVO.class);//转成uservo类型的数据
        String username = userVO.getName();
        //根据username查询数据库中对应的数据
        TbAddressExample example = new TbAddressExample();
        example.createCriteria().andUserIdEqualTo(username);
        List<TbAddress> tbAddresses = addressMapper.selectByExample(example);
        List<AddressVO> collect = tbAddresses.stream().map(tbAddress -> {
            //查询对应的城市名
            TbProvincesExample provincesExample = new TbProvincesExample();
            provincesExample.createCriteria().andProvinceidEqualTo(tbAddress.getProvinceId());
            List<TbProvinces> tbProvincesList = provincesMapper.selectByExample(provincesExample);
            TbProvinces provinces = tbProvincesList.get(0);

            TbCitiesExample citiesExample = new TbCitiesExample();
            citiesExample.createCriteria().andCityidEqualTo(tbAddress.getCityId());
            List<TbCities> tbCitiesList = citiesMapper.selectByExample(citiesExample);
            TbCities tbCities = tbCitiesList.get(0);

            TbAreasExample areasExample = new TbAreasExample();
            areasExample.createCriteria().andAreaidEqualTo(tbAddress.getTownId());
            List<TbAreas> tbAreasList = areasMapper.selectByExample(areasExample);
            TbAreas tbAreas = tbAreasList.get(0);

            AddressVO vo = new AddressVO();
            BeanUtils.copyProperties(tbAddress, vo);
            vo.setProvinceName(provinces.getProvince());
            vo.setCityName(tbCities.getCity());
            vo.setTownName(tbAreas.getArea());
            return vo;
        }).collect(Collectors.toList());
        return collect;
    }

    @Override
    public AddressVO selectById(Long id) {
        //根据id查询数据
        TbAddress tbAddress = addressMapper.selectByPrimaryKey(id);
        AddressVO vo = null;
        if (tbAddress != null){
            vo = new AddressVO();
            BeanUtils.copyProperties(tbAddress,vo);
        }
        return vo;
    }
}
